package project.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TActions;
import project.service.TActionsService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;

/**
 * <p>
 *  社区表 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-14
 */
@RestController
@RequestMapping("/api/actions/v1")
public class TActionsController {
    @Resource
    private TActionsService actionsService;

    @RequestMapping(value = "/wx/saveOrUpdate",method = RequestMethod.POST)
    public JSONObject saveOrUpdate(@RequestBody JSONObject jsonObject){
        TActions actions = jsonObject.to(TActions.class);
        if(StringUtils.isBlank(actions.getcId())){
            actions.setcId(UUIDUtil.getUUID());
        }
        return ResponseUtil.getSuccessResponseBody(actionsService.saveOrUpdate(actions));
    }

    @RequestMapping(value = "/wx/select",method = RequestMethod.POST)
    public JSONObject select(@RequestBody JSONObject jsonObject){
        String openId = jsonObject.getString("openId");
        QueryWrapper<TActions> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(openId)){
            queryWrapper.lambda().eq(TActions::getcOpenId,openId);
        }
        queryWrapper.lambda().orderByDesc(TActions::getDtCreate);
        return ResponseUtil.getSuccessResponseBody(actionsService.list(queryWrapper));
    }

    @RequestMapping(value = "/wx/remove",method = RequestMethod.POST)
    public JSONObject remove(@RequestBody JSONObject jsonObject){
        return ResponseUtil.getSuccessResponseBody(actionsService.removeById(jsonObject.getString("cId")));
    }

    @RequestMapping(value = "/wx/getById",method = RequestMethod.POST)
    public JSONObject getById(@RequestBody JSONObject jsonObject){
        return ResponseUtil.getSuccessResponseBody(actionsService.getById(jsonObject.getString("cId")));
    }

    /**
     *  分页查询数据
     */
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public JSONObject selectAction(@RequestBody JSONObject param){
        // 获取页数
        Integer current = param.getInteger("current");

        String nickName = param.getString("nickName");
        String text = param.getString("text");
        Page<TActions> page = new Page<TActions>();
        page.setSize(10);
        if(current != null){
            page.setCurrent(current);
        }else{
            page.setCurrent(1);
        }
        // 构建查询条件
        QueryWrapper<TActions> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(nickName)){
            wrapper.lambda().like(TActions::getcNickName,nickName);
        }
        if(StringUtils.isNotBlank(text)){
            wrapper.lambda().like(TActions::getcText,text);
        }
        wrapper.lambda().orderByDesc(TActions::getDtCreate);
        // 获取查询返回结果
        Page<TActions> pageSelect = actionsService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list",pageSelect.getRecords());
        object.put("total",pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }
}
